$(function () {
    var markid = getUrlString('markid');
    var source = getUrlString('source');
    var kutoysids = getUrlString('kutoysids');
    // var markid = '3b70887e5d841ff8ab7388ea5f41a5af';
    var page = -1,totalSize = 0;
    var mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#addressList').empty();//清空容器
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                addressList(page);
            }
        }
    });

    function addressList (page) {
        var data = {
            'markid':markid,
            'page':page
        };
        getData('post','/niu/apiv3/fahuo_address_getlist.php',data,function (data) {
            console.log(data);
            var append = '';
            $(data.data).each(function () {
               append += '<li>' +
                   ' <input type="hidden" class="id" value="'+this.id+'"> '+
                   ' <input type="hidden" class="name" value="'+this.name+'"> '+
                   ' <input type="hidden" class="phone" value="'+this.phone+'"> '+
                   ' <input type="hidden" class="adress" value="'+this.adress+'"> '+
                   ' <input type="hidden" class="local1name" value="'+this.local1name+'"> '+
                   ' <input type="hidden" class="local2name" value="'+this.local2name+'"> '+
                   ' <input type="hidden" class="local3name" value="'+this.local3name+'"> '+
                   ' <input type="hidden" class="local1" value="'+this.local1+'"> '+
                   ' <input type="hidden" class="local2" value="'+this.local2+'"> '+
                   ' <input type="hidden" class="local3" value="'+this.local3+'"> '+
                   '                    <div class="name">'+this.name+'</div>' +
                   '                    <div class="phoneNum">'+this.phone+'</div>' +
                   '                    <div class="address">'+this.local1name+this.local2name+this.local3name+this.adress+'</div>' +
                   '                </li>' 
            });
            $('#addressList').append(append);
            $('#addressList li').click(function () {
                let id = $(this).find('.id').val();
                let name = $(this).find('.name').val();
                let phone = $(this).find('.phone').val();
                let local1name = $(this).find('.local1name').val();
                let local2name = $(this).find('.local2name').val();
                let local3name = $(this).find('.local3name').val();
                let local1 = $(this).find('.local1').val();
                let local2 = $(this).find('.local2').val();
                let local3 = $(this).find('.local3').val();
                let adress = $(this).find('.adress').val();
                if (source === 'shipments'){
                    window.location.href = '../../view/shipments/shipmentsOrder.html?markid='+markid+'&kutoysids='+kutoysids+'&id='+id+'&name='+name+'&phone='+phone+
                        '&local1name='+local1name+'&local2name='+local2name+'&local3name='+local3name+
                    '&local1='+local1+'&local2='+local2+'&local3='+local3+'&adress='+adress;
                }else {
                    window.location.href = 'dancool://web='+urlView+'addressAdmin/upDataAddress.html?markid='+markid+'&id='+id+'&name='+name+'&phone='+phone+
                        '&local1name='+local1name+'&local2name='+local2name+'&local3name='+local3name+'&adress='+adress;
                }
            });
            //总条数
            totalSize = totalSize + 10;
            if (totalSize > data.datacount)
                mRefresh.endUpLoading(true);
            else{
                mRefresh.endUpLoading(false);
            }
        })
    }
    $('#orderBtn').click(function () {
        if (source === 'shipments'){
            window.location.href = 'addAddress.html?markid='+markid+'&kutoysids='+kutoysids+'&source=shipments';
        }else {
            window.location.href = 'dancool://web='+urlView+'addressAdmin/addAddress.html?markid='+markid
        }
    })
});